﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace Framework.Serializer
{
    class BinarySerializer : BytesSerializeBase
    {
        static BinaryFormatter formatter = new BinaryFormatter();

        public override T DeSerialize<T>(byte[] source)
        {
            if (source != null)
            {
                using (MemoryStream stream = new MemoryStream(source))
                {
                    return (T)formatter.Deserialize(stream);
                }
            }
            return default(T);
        }

        public override byte[] Serialize(object obj)
        {
            if (obj != null)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    formatter.Serialize(stream, obj);
                    return stream.ToArray();
                }
            }
            return null;
        }
    }
}
